package com.example.demo.zaqizabatest.leetcode.day1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

public class MinStack {
    Stack<Integer> stack;
    List<Integer> minList;
    /** initialize your data structure here. */
    public MinStack() {
        stack = new Stack<>();
        minList = new ArrayList<>();
    }

    public void push(int x) {
        minList.add(x);
        Collections.sort(minList);
        stack.push(x);
    }

    public void pop() {
        Integer pop = stack.pop();
        minList.remove(pop);
        Collections.sort(minList);
    }

    public int top() {
        return stack.peek();
    }

    public int min() {
        if (minList.size()>0){
            return minList.get(0);
        } else {
            throw new RuntimeException("栈中无值！");
        }

    }
}
